package com.adam.management.controller;

import com.adam.management.pojo.User;
import com.adam.management.service.UserService;
import com.adam.management.util.AjaxUtil;
import com.adam.management.util.SessionParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/user")
public class userController {
    @Autowired
    UserService userService;

    @RequestMapping("/main")
    public String main(HttpSession session, Map<String,Object> map){
        Object attribute = session.getAttribute(SessionParameter.USER_INFO);
        if(attribute!=null && attribute instanceof User){
            User user = (User)attribute;
            User dbUser = userService.findByName(user);
//            System.out.println("test-----------------------");
//            System.out.println(dbUser.getPhone());
//            System.out.println(dbUser.getCreatedate());
            map.put("user",dbUser);
        }
        return "main";
    }

    @RequestMapping("/personal")
    public String personal(HttpSession session, Map<String,Object> map){
        Object attribute = session.getAttribute(SessionParameter.USER_INFO);
        if(attribute!=null && attribute instanceof User){
            User user = (User)attribute;
            User dbUser = userService.findByName(user);
//            System.out.println("test-----------------------");
//            System.out.println(dbUser.getPhone());
//            System.out.println(dbUser.getCreatedate());
            map.put("user",dbUser);
        }
        return "personal";
    }
    @RequestMapping("/login")
    public String login(){
        return "signin";
    }
    @RequestMapping("/dologin")
    @ResponseBody
    public AjaxUtil doLogin(User user , HttpSession session){
        AjaxUtil ajaxUtil = new AjaxUtil();
        if(!Pattern.matches("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", user.getUsername())){
            ajaxUtil.setMsg("badUsername");
            return ajaxUtil;
        }
        User dbUser = userService.findByNameAndPassWord(user);
        if(dbUser!=null){
            session.setAttribute(SessionParameter.USER_INFO,dbUser);
            ajaxUtil.setMsg("ok");
        }else{
            ajaxUtil.setMsg("error");
        }

        return ajaxUtil;
    }
    @RequestMapping("register")
    public String register(){
        return "register";
    }
    @RequestMapping("doregister")
    @ResponseBody
    public AjaxUtil doRegister(User user){
        AjaxUtil ajaxUtil = new AjaxUtil();
        if(!Pattern.matches("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", user.getUsername())){
            ajaxUtil.setMsg("badUsername");
            return ajaxUtil;
        }
        User dbUser = userService.findByName(user);
        if(dbUser==null){
            LocalDate date = LocalDate.now(); // get the current date
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            user.setCreatedate(date.format(formatter));
            userService.createUser(user);
            ajaxUtil.setMsg("ok");
        }else{
            ajaxUtil.setMsg("hasUsername");
        }

        return ajaxUtil;
    }
    @RequestMapping("logout")
    public String logOut(HttpSession session){
        session.removeAttribute(SessionParameter.USER_INFO);
        return "signin";
    }
    @RequestMapping("manage")
    public String manage(Map<String,Object> map, HttpSession session, HttpServletRequest request){
        Object attribute = session.getAttribute(SessionParameter.USER_INFO);
        if(attribute!=null && attribute instanceof User){
            User user = (User)attribute;
            map.put("user",user);
        }
        String searchtype = request.getParameter("searchtype");
        String searchvalue = request.getParameter("searchvalue");
        User search = new User();
        if(searchtype!=null&&searchtype.trim().length()>0 &&searchvalue!=null&&searchvalue.trim().length()>0){
            if(searchtype.equals("1")){
                search.setUsername(searchvalue);
            }else if(searchtype.equals("2")){
                search.setNickname(searchvalue);
            }
        }

        int currentPage = 1;
        int pageSize = 5;
        int total = userService.count(search);
        int firstPage = total%pageSize;
        if(firstPage==0){
            firstPage=0;
        }else{
            firstPage=1;
        }
        int pageTotal = total/pageSize+firstPage;
        String currentPagestr = request.getParameter("currentPage");
        if(currentPagestr!=null){
            currentPage = Integer.parseInt(currentPagestr);
        }
//        List<User> userList = userService.findAll();
        List<User> userList = userService.findAllByPage(currentPage,pageSize,search);
        map.put("userList",userList);
        map.put("currentPage",currentPage);
        map.put("total",total);
        map.put("pageTotal",pageTotal);
        map.put("searchtype",searchtype);
        map.put("searchvalue",searchvalue);
        return "manage";
    }
    @RequestMapping("edit")
    public String edit(HttpSession session, Map<String,Object> map){
        Object attribute = session.getAttribute(SessionParameter.USER_INFO);
        if(attribute!=null && attribute instanceof User){
            User user = (User)attribute;
            map.put("user",user);
            User dbUser = userService.findByName(user);
            map.put("user_edi",dbUser);
        }
        AjaxUtil ajaxUtil = new AjaxUtil();
        ajaxUtil.setFlag("0");
        map.put("ajax",ajaxUtil);

        return "edit";
    }
    @RequestMapping("edituser")
    public String editUser(User user, HttpSession session,AjaxUtil ajax){
        userService.updateUser(user);

        if(ajax.getFlag().equals("1")){

            System.out.println("--------------------------");
            System.out.println(ajax.getFlag());
            return "redirect:/user/manage";
        }
        return "redirect:/user/personal";

    }
    @RequestMapping("delete")
    public String delete(int id){
        userService.delete(id);
        return "redirect:/user/manage";
    }
    @RequestMapping("edit_by_id")
    public String editById(int id,Map<String,Object> map,HttpSession session,RedirectAttributes redirectAttributes){
        Object attribute = session.getAttribute(SessionParameter.USER_INFO);
        if(attribute!=null && attribute instanceof User){
            User logUser = (User)attribute;
            map.put("user",logUser);
        }
        User user = userService.findById(id);
        map.put("user_edi",user);
        AjaxUtil ajaxUtil = new AjaxUtil();
        ajaxUtil.setFlag("1");
        map.put("ajax",ajaxUtil);
        return "edit";
    }
    @RequestMapping("adduser")
    public String adduser(HttpSession session,Map<String,Object> map){
        Object attribute = session.getAttribute(SessionParameter.USER_INFO);
        if(attribute!=null && attribute instanceof User){
            User logUser = (User)attribute;
            map.put("user",logUser);
        }
        return "adduser";
    }
    @RequestMapping("add")
    public String add(User user){

        LocalDate date = LocalDate.now(); // get the current date
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        user.setCreatedate(date.format(formatter));
        userService.createUser(user);
        return "redirect:/user/manage";
    }
}
